package com.huali.dao;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class BaseDao {
    /*
    * 数据源对象
    * */
    private static DataSource dataSource = null;

    static {
        try {
            //配置文件对象
            Properties properties = new Properties();
            //加载配置文件
            properties.load(BaseDao.class.getClassLoader().getResourceAsStream("druid.properties"));
            //获取数据源对象
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
    * 获取数据库连接对象
     *
     * @return connection
     * @throws SQLException sql异常
     * */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    public static int executeUpdata(String sql,Object...data){
        //数据库连接对象
        Connection conn = null;
        //sql执行对象
        PreparedStatement ps = null;
        //返回值
        int res = 0;

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i + 1,data[i]);
            }
            //执行sql获取返回结果
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(conn,ps,null);
        }
        return res;
    }

    public static List<Map<String,Object>> executeQuery(String sql,Object...data){
        //数据库连接对象
        Connection conn = null;
        //sql执行对象
        PreparedStatement ps = null;
        //返回结果
        ResultSet rs = null;
        //返回对象
        List<Map<String, Object>> list = new ArrayList<>();

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i+1,data[i]);
            }

            rs = ps.executeQuery();
            int count = rs.getMetaData().getColumnCount();

            while (rs.next()){
                Map<String,Object> map = new HashMap<>();
                for (int i = 0; i <= count; i++) {
                    //获取数据的字段名
                    String name = rs.getMetaData().getColumnLabel(i);
                    //获取数据值
                    Object values = rs.getObject(i);
                    //将数据存放在map对象中
                    map.put(name,values);
                }
                //将当前数据存
                list.add(map);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(conn,ps,rs);
        }

        return list;
    }

    /**
    *
     * */
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        //inn
        if (conn != null){
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (ps != null){
            try {
                ps.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (rs != null){
            try {
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }



}
